<template>
  <el-card style="margin-top: 20px">
    <div slot="header" class="clearfix">
      <span class="title">款项进度</span>
    </div>
    <div style="position: relative">
      <div :style="{ height: height, width: width }" ref="canvas" />
    </div>
  </el-card>
</template>
<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../../dashboard/mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "200px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.canvas, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions(v) {
      this.chart.setOption({
        tooltip: {
          trigger: "item",
        },
        series: [
          {
            name: "金额",
            type: "pie",
            radius: "50%",
            data: [
              { value: v.getMoney, name: "已收款" },
              { value: v.leaveMoney, name: "未收款" },
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.canvas {
  width: 200px;
  height: 200px;
}
.box-card {
  margin-top: 20px;
}
</style>
